<template>
  <a-modal
      :confirmLoading="confirmLoading"
      :maskClosable="false"
      :title="title"
      :visible="visible"
      :width="720"
      @cancel="handleCancel"
      @ok="handleSubmit"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="渠道状态">
          <a-radio-group
              v-decorator="[`status`, { initialValue: Status.ENABLED.key, rules: [{ required: true }] }]"
          >
            <a-radio
                v-for="(item, index) in Status.data"
                :key="index"
                :value="item.key"
            >{{ item.name }}
            </a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="备注">
          <a-input v-decorator="['remark']" placeholder="请输入备注"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import * as Api from '@/api/pay/channel'
import StatusEnum from '@/common/enum/Status'
import _ from 'lodash'

export default {
  data() {
    return {
      // 对话框标题
      title: '',
      // 标签布局属性
      labelCol: {
        span: 7
      },
      // 输入框布局属性
      wrapperCol: {
        span: 13
      },
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      confirmLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
      // 当前记录
      record: {
        appId: '',
        code: '',
        status: undefined,
        feeRate: 0,
        remark: '',
        config: {
          name: 'mock-conf'
        }
      }
    }
  },
  computed: {
    Status() {
      return StatusEnum
    }
  },
  methods: {
    // 显示对话框
    show(appId, code) {
      this.visible = true
      this.resetForm(appId, code)

      Api.detail(undefined, {appId, code}).then(data => {
        if (data && data.id) {
          this.record = data
          this.record.config = JSON.parse(this.record.config || '{}')
          this.title = '编辑支付渠道'
          this.setFieldsValue()
        } else {
          this.title = '创建支付渠道'
          this.setFieldsValue()
        }
      })
    },

    // 设置默认值
    setFieldsValue() {
      const {$nextTick, form: {setFieldsValue}} = this
      $nextTick(() => {
        setFieldsValue(_.pick(this.record, ['status', 'remark']))
      })
    },

    resetForm(appId, code) {
      const {$nextTick, Status, form: {resetFields}} = this
      this.record = {
        appId: appId,
        code: code,
        status: Status.ENABLED.key,
        remark: '',
        feeRate: 0,
        config: {
          name: 'mock-conf'
        }
      }
      $nextTick(() => {
        resetFields()
      })
    },

    // 确认按钮
    handleSubmit(e) {
      e.preventDefault()
      // 表单验证
      const {form: {validateFields}} = this
      validateFields((errors, values) => {
        // 提交到后端api
        if (!errors) {
          this.onFormSubmit(values)
        }
      })
    },

    // 关闭对话框事件
    handleCancel() {
      this.visible = false
      this.form.resetFields()
    },

    // 提交到后端api
    onFormSubmit(values) {
      this.confirmLoading = true
      const params = {...this.record, ...values}
      params.config = JSON.stringify(params.config || {})
      const api = this.record && this.record.id ? Api.edit(this.record.id, params) : Api.add(params)
      api
          .then(() => {
            this.$message.success('操作成功', 1.5)
            this.handleCancel()
            this.$emit('handleSubmit', params)
          })
          .finally(() => {
            this.confirmLoading = false
          })
    }
  }
}
</script>
